package com.wzy.Controller.admin;

import com.wzy.entity.Video;
import com.wzy.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Set;

/**
 * @author : Zht
 * @create : 2021/2/25
 * @describe:
 */
@Controller
@RequestMapping("/admin")
public class VideosController {

    @Autowired
    private VideoService videoService;

    //推荐列表
    @GetMapping("/video")
    public String index(Model model, @RequestParam(value = "page", defaultValue = "1") Integer page) {
        model.addAttribute("pager", this.videoService.list(page));
        return "admin/video/videos";
    }

    //跳到推荐
    @GetMapping("/video/input")
    public String input() {
        return "admin/video/videos-input";
    }

    //新增推荐
    @PostMapping("/video")
    public String append(Video video, RedirectAttributes attributes, Model model, @RequestParam(value = "page", defaultValue = "1") Integer page) {
        if (this.videoService.append(video)) {
            attributes.addFlashAttribute("messages", "添加成功");
        } else {
            attributes.addFlashAttribute("messages", "添加失败");
        }
        return "redirect:/admin/video";
    }

    //跳到编辑推荐
    @GetMapping("/video/{videoId}/input")
    public String edit(@RequestParam("videoId") Long videoId, Model model) {
        model.addAttribute("video", this.videoService.detail(videoId));
        return "admin/video/videos-modify";
    }

    //编辑推荐
    @PostMapping("/video/{videoId}")
    public String editPost(Video video, RedirectAttributes attributes) {
        if (this.videoService.modify(video)) {
            attributes.addFlashAttribute("messages", "编辑成功");
        } else {
            attributes.addFlashAttribute("messages", "编辑失败");
        }
        return "redirect:/admin/video";
    }

    //删除推荐
    @RequestMapping("/video/delete")
    public String delete(Long videoId, RedirectAttributes attributes) {
        this.videoService.remove(videoId);
        attributes.addFlashAttribute("messages", "删除成功");
        return "redirect:/admin/video";
    }

}
